#include "manager.h"
#include "common_utilities.h"
#include <QtDebug>
#include <QSettings>
#include <QTime>
#include <QStringList>
#include <QDir>

Manager* Manager::manager = 0;

Manager::Manager()
    : m_language("en")
    , m_category("fruites")
{
    QTime midnight(0, 0, 0);
    qsrand(midnight.secsTo(QTime::currentTime()));
}

Manager* Manager::getManager()
{
    if (0 == manager)
    {
        manager = new Manager();
    }
    return manager;
}


Test Manager::getNextTest()
{
    int k = qrand() % m_maxProbability;
    int i = 0;
    while (k > 0 && i < listOfOptions.size()) {
        int p = listOfOptions[i].getProbability();
        if ((k - p) < 0) {
            return generateTest(i);
        }
        k -= p;
        ++i;
    }
    return generateTest(0);
}

QVector<int> Manager::generateUniqueNumbers(int a, int b, int c, QVector<int> d) const
{
    QVector<int> vec;

    while (vec.size() < c) {
        int k = a + qrand() % (b - a);
        while(vec.contains(k) || d.contains(k)) {
            k = a + qrand() % (b - a);
        }
        vec.push_back(k);
    }
    return vec;
}

Test Manager::generateTest(int id)
{
    Test t;
    QVector<int> a = generateUniqueNumbers(0, listOfOptions.size(), 5, QVector<int>() << id);
    foreach(const int &i, a) {
        Option o = listOfOptions[i];
        t.optionsList.push_back(o);
    }
    int k = qrand() % 5;
    t.optionsList.insert(k, listOfOptions[id]);
    t.currentOptionId = k;
    t.id = id;
    return t;
}

void Manager::updateTestStatistics(int i, bool b)
{
    m_maxProbability -= listOfOptions[i].getProbability();

    listOfOptions[i].suggestedCount++;
    if (b) {
        listOfOptions[i].rightAnswerCount++;
    }

    m_maxProbability += listOfOptions[i].getProbability();

    QString p = QDir::current().absolutePath();
    QSettings stat(p + "/tests/languages/" + m_language + "/" + m_category + "_statistics.ini", QSettings::IniFormat);
    stat.beginGroup(QString::number(listOfOptions[i].id));
    stat.setValue("suggestedCount", listOfOptions[i].suggestedCount);
    stat.setValue("rightAnswerCount", listOfOptions[i].rightAnswerCount);
    stat.endGroup();
}

void Manager::loadOptions(const QString& category, const QString& language)
{
    listOfOptions.clear();
    m_maxProbability = 0;
    QString p = QDir::current().absolutePath();
    QSettings main(p + "/tests/categories/" + category + ".ini", QSettings::IniFormat);
    QSettings lang(p + "/tests/languages/" + language + "/" + category + ".ini", QSettings::IniFormat);
    QSettings stat(p + "/tests/languages/" + language + "/" + category + "_statistics.ini", QSettings::IniFormat);
    lang.setIniCodec("UTF-8");
    QStringList list = main.childGroups();
    foreach(const QString &key, list) {
        Option o;
        o.id = key.toInt();
        main.beginGroup(key);
        stat.beginGroup(key);
        o.category = category;
        o.language = language;
        o.path = p + "/images/" + category + "/" + main.value("path").toString();
        o.suggestedCount = stat.value("suggestedCount").toInt();
        o.rightAnswerCount = stat.value("rightAnswerCount").toInt();
        main.endGroup();
        stat.endGroup();
        o.text = lang.value(key).toString();
        listOfOptions.push_back(o);
        m_maxProbability += o.getProbability();
    }
}

void Manager::loadTests()
{
    loadOptions(m_category, m_language);
}

void Manager::print()
{
    qDebug() << "!!! Manager !!!";
    for (int i = 0; i < listOfOptions.size(); ++i) {
        Option a = listOfOptions[i];
        printOption(a);
    }
}
